<template>
  <div v-if="visible" class="user-info-overlay" @click="handleOverlayClick">
    <div class="user-info-container" @click.stop>
      <div class="dialog-header">
        <h2 class="dialog-title">个人信息</h2>
        <button class="close-btn" @click="close" type="button">×</button>
      </div>
      
      <div class="dialog-content">
        <div class="user-avatar-section">
          <div class="large-user-avatar">
            {{ userInfo?.account?.charAt(0)?.toUpperCase() || 'U' }}
          </div>
          <p class="user-account">{{ userInfo?.account || '未知用户' }}</p>
        </div>
        
        <div class="user-details">
          <div class="detail-item">
            <label>账号</label>
            <span>{{ userInfo?.account || '未设置' }}</span>
          </div>
          
          <div class="detail-item nickname-item">
            <label>昵称</label>
            <div class="nickname-content">
              <input 
                v-if="isEditingNickname"
                v-model="editNickname"
                class="nickname-input"
                type="text"
                maxlength="20"
                placeholder="请输入昵称"
                @keyup.enter="saveNickname"
                @keyup.escape="cancelEdit"
                ref="nicknameInput"
              />
              <span v-else class="nickname-display">{{ userInfo?.nickname || '未设置' }}</span>
              <div class="nickname-actions">
                <button 
                  v-if="!isEditingNickname"
                  @click="startEditNickname"
                  class="edit-btn"
                  type="button"
                  title="编辑昵称"
                >
                  <i class="iconfont icon-bianji"></i>
                </button>
                <template v-else>
                  <button 
                    @click="saveNickname"
                    class="save-btn"
                    type="button"
                    title="保存"
                    :disabled="isUpdating"
                  >
                    <i v-if="!isUpdating" class="iconfont icon-duigou"></i>
                    <span v-else class="updating-spinner">⏳</span>
                  </button>
                  <button 
                    @click="cancelEdit"
                    class="cancel-btn"
                    type="button"
                    title="取消"
                    :disabled="isUpdating"
                  >
                    <i class="iconfont icon-cha"></i>
                  </button>
                </template>
              </div>
            </div>
          </div>
          
          <div class="detail-item">
            <label>邮箱</label>
            <span>{{ userInfo?.email || '未设置' }}</span>
          </div>
          
          <div class="detail-item">
            <label>手机号</label>
            <span>{{ userInfo?.phone || '未设置' }}</span>
          </div>
        </div>
        
        <div class="dialog-actions">
          <button class="action-btn secondary" @click="close" type="button">
            关闭
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick, inject } from 'vue'
import { invoke } from '@tauri-apps/api/core'
import type { UserInfo } from '../utils/userStore'
import { useUserStore } from '../utils/userStore'

interface Props {
  visible: boolean
  userInfo: UserInfo | null
}

interface Emits {
  (e: 'update:visible', value: boolean): void
  (e: 'userUpdated'): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const showMessageBar = inject('showMessageBar') as (message: string, type?: 'info' | 'warning' | 'error') => void
const userStore = useUserStore()

const isEditingNickname = ref(false)
const editNickname = ref('')
const isUpdating = ref(false)
const nicknameInput = ref<HTMLInputElement>()

const close = () => {
  emit('update:visible', false)
  cancelEdit()
}

const handleOverlayClick = () => {
  close()
}

const startEditNickname = async () => {
  editNickname.value = props.userInfo?.nickname || ''
  isEditingNickname.value = true
  
  await nextTick()
  nicknameInput.value?.focus()
  nicknameInput.value?.select()
}

const cancelEdit = () => {
  isEditingNickname.value = false
  editNickname.value = ''
}

const saveNickname = async () => {
  if (isUpdating.value) return
  
  const trimmedNickname = editNickname.value.trim()
  if (!trimmedNickname) {
    showMessageBar('昵称不能为空', 'error')
    return
  }
  
  if (trimmedNickname === props.userInfo?.nickname) {
    cancelEdit()
    return
  }
  
  try {
    isUpdating.value = true
    
    const result = await invoke('update_user_info', {
      nickName: trimmedNickname
    }) as boolean
    
    if (result) {
      showMessageBar('昵称更新成功', 'info')
      
      // 刷新前端用户信息显示
      await userStore.refreshUserInfo()
      
      emit('userUpdated')
      cancelEdit()
    } else {
      showMessageBar('昵称更新失败', 'error')
    }
  } catch (error) {
    console.error('更新昵称失败:', error)
    showMessageBar(`更新失败: ${error}`, 'error')
  } finally {
    isUpdating.value = false
  }
}

// 格式化日期 (暂未使用，预留功能)
// const formatDate = (dateString: string | undefined): string => {
//   if (!dateString) return '未知'
//   try {
//     const date = new Date(dateString)
//     return date.toLocaleString('zh-CN', {
//       year: 'numeric',
//       month: '2-digit',
//       day: '2-digit',
//       hour: '2-digit',
//       minute: '2-digit'
//     })
//   } catch (error) {
//     return '格式错误'
//   }
// }
</script>

<style scoped>
.user-info-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease;
}

.user-info-container {
  background: var(--card-bg, #ffffff);
  border-radius: var(--radius-lg, 12px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
  width: 100%;
  max-width: 480px;
  margin: 20px;
  animation: slideUp 0.3s ease;
  max-height: 90vh;
  overflow-y: auto;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px 24px 0 24px;
}

.dialog-title {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: var(--text-primary, #333);
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  color: var(--text-secondary, #666);
  cursor: pointer;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.2s ease;
}

.close-btn:hover {
  background: var(--bg-hover, #f5f5f5);
  color: var(--text-primary, #333);
}

.dialog-content {
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.user-avatar-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.large-user-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--header-bg, #2c7a7b), #319795);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  font-weight: bold;
  color: white;
  box-shadow: 0 4px 12px rgba(44, 122, 123, 0.3);
}

.user-account {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary, #333);
  margin: 0;
}

.user-details {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--bg-secondary, #f8fafc);
  border-radius: var(--radius-md, 8px);
  border: 1px solid var(--border-light, #e2e8f0);
}

.detail-item label {
  font-weight: 500;
  color: var(--text-secondary, #666);
  min-width: 80px;
}

.detail-item span {
  color: var(--text-primary, #333);
  text-align: right;
  flex: 1;
}

.nickname-item {
  align-items: flex-start;
}

.nickname-content {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  justify-content: flex-end;
}

.nickname-display {
  flex: 1;
  text-align: right;
}

.nickname-input {
  flex: 1;
  border: 1px solid var(--border-color, #e2e8f0);
  border-radius: var(--radius-sm, 4px);
  padding: 6px 8px;
  font-size: 14px;
  color: var(--text-primary, #333);
  background: var(--card-bg, #ffffff);
  outline: none;
  text-align: right;
}

.nickname-input:focus {
  border-color: var(--header-bg, #2c7a7b);
  box-shadow: 0 0 0 2px rgba(44, 122, 123, 0.1);
}

.nickname-actions {
  display: flex;
  gap: 4px;
}

.edit-btn,
.save-btn,
.cancel-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  font-size: 14px;
  transition: all 0.2s ease;
  min-width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary, #666);
}

.edit-btn .iconfont,
.save-btn .iconfont,
.cancel-btn .iconfont {
  font-size: 14px;
}

.updating-spinner {
  font-size: 12px;
}

.edit-btn:hover {
  background: var(--bg-hover, #f5f5f5);
  transform: scale(1.1);
}

.save-btn:hover:not(:disabled) {
  background: rgba(34, 197, 94, 0.1);
  transform: scale(1.1);
}

.cancel-btn:hover:not(:disabled) {
  background: rgba(239, 68, 68, 0.1);
  transform: scale(1.1);
}

.save-btn:disabled,
.cancel-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.dialog-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 8px;
}

.action-btn {
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius-md, 8px);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 80px;
}

.action-btn.secondary {
  background: var(--bg-secondary, #f8fafc);
  color: var(--text-secondary, #666);
  border: 1px solid var(--border-color, #e2e8f0);
}

.action-btn.secondary:hover {
  background: var(--bg-hover, #e2e8f0);
  color: var(--text-primary, #333);
}

.action-btn.primary {
  background: linear-gradient(135deg, var(--header-bg, #2c7a7b), #319795);
  color: white;
}

.action-btn.primary:hover {
  background: linear-gradient(135deg, #319795, #2dd4bf);
  box-shadow: 0 4px 12px rgba(44, 122, 123, 0.25);
  transform: translateY(-1px);
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式适配 */
@media (max-width: 480px) {
  .user-info-container {
    margin: 10px;
    max-width: none;
  }
  
  .dialog-header, .dialog-content {
    padding-left: 20px;
    padding-right: 20px;
  }
  
  .dialog-title {
    font-size: 20px;
  }
  
  .large-user-avatar {
    width: 64px;
    height: 64px;
    font-size: 24px;
  }
  
  .detail-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  
  .detail-item span {
    text-align: left;
  }
  
  .dialog-actions {
    flex-direction: column;
  }
  
  .action-btn {
    width: 100%;
  }
}

/* VIP区域样式 */
.vip-section {
  border-top: 1px solid var(--border-color, #e2e8f0);
  padding-top: 20px;
  margin-top: 20px;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary, #333);
  margin: 0;
}

.section-icon {
  font-size: 18px;
}

.refresh-btn {
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.2s ease;
  opacity: 0.6;
}

.refresh-btn:hover {
  opacity: 1;
  background: var(--bg-hover, #f5f5f5);
  transform: rotate(90deg);
}

.refreshing-indicator {
  font-size: 16px;
  opacity: 0.6;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.vip-status-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-radius: var(--radius-lg, 12px);
  border: 2px solid;
  margin-bottom: 16px;
  transition: all 0.3s ease;
}

.vip-status-card.status-free {
  border-color: #e2e8f0;
  background: linear-gradient(135deg, #f8fafc, #edf2f7);
}

.vip-status-card.status-active {
  border-color: #f6ad55;
  background: linear-gradient(135deg, #fffaf0, #fed7aa);
}

.vip-status-card.status-warning {
  border-color: #f6ad55;
  background: linear-gradient(135deg, #fffbeb, #fde68a);
}

.vip-status-card.status-expired {
  border-color: #fc8181;
  background: linear-gradient(135deg, #fef5e7, #fed7d7);
}

.vip-main-info {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
}

.vip-icon-large {
  font-size: 32px;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.5);
}

.vip-details {
  flex: 1;
}

.vip-type {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-primary, #333);
  margin-bottom: 2px;
}

.vip-status-text {
  font-size: 14px;
  color: var(--text-secondary, #666);
  margin-bottom: 4px;
}

.vip-expire-time {
  font-size: 12px;
  color: var(--text-secondary, #666);
  margin-bottom: 2px;
}

.vip-remaining {
  font-size: 13px;
  font-weight: 500;
}

.text-warning {
  color: #d69e2e;
}

.text-danger {
  color: #e53e3e;
}

.text-primary {
  color: var(--primary-color, #2c7a7b);
  font-weight: 500;
}

.upgrade-btn {
  padding: 8px 16px;
  background: linear-gradient(135deg, #2c7a7b, #319795);
  color: white;
  border: none;
  border-radius: var(--radius-md, 8px);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
}

.upgrade-btn:hover {
  background: linear-gradient(135deg, #319795, #2dd4bf);
  box-shadow: 0 4px 12px rgba(44, 122, 123, 0.25);
  transform: translateY(-1px);
}

.vip-benefits {
  margin-top: 16px;
}

.benefits-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary, #333);
  margin: 0 0 12px 0;
}

.benefits-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 12px;
}

.benefit-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 12px 8px;
  background: rgba(255, 255, 255, 0.5);
  border-radius: var(--radius-md, 8px);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.benefit-icon {
  font-size: 20px;
  margin-bottom: 6px;
}

.benefit-label {
  font-size: 12px;
  color: var(--text-secondary, #666);
  margin-bottom: 4px;
}

.benefit-value {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-primary, #333);
}

/* 响应式适配 */
@media (max-width: 480px) {
  .vip-status-card {
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
  
  .vip-main-info {
    flex-direction: column;
    text-align: center;
  }
  
  .upgrade-btn {
    width: 100%;
  }
  
  .benefits-grid {
    grid-template-columns: 1fr;
  }
}

/* 暗色模式支持 */
@media (prefers-color-scheme: dark) {
  .user-info-container {
    --card-bg: #2d2d2d;
    --text-primary: #e6e6e6;
    --text-secondary: #999999;
    --bg-secondary: #3a3a3a;
    --bg-hover: #3d3d3d;
    --border-color: #3d3d3d;
    --border-light: #404040;
  }
  
  .vip-status-card.status-free {
    border-color: #3d3d3d;
    background: linear-gradient(135deg, #2a2a2a, #333);
  }
  
  .vip-status-card.status-active {
    border-color: #f6ad55;
    background: linear-gradient(135deg, #2d2416, #3a2f1b);
  }
  
  .vip-status-card.status-warning {
    border-color: #f6ad55;
    background: linear-gradient(135deg, #332a1b, #3d2d1f);
  }
  
  .vip-status-card.status-expired {
    border-color: #fc8181;
    background: linear-gradient(135deg, #2d1b1b, #3d1f1f);
  }
  
  .benefit-item {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.1);
  }
}
</style>